/*
 * eID Diagnostics Project.
 * Copyright (C) 2010 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see 
 * http://www.gnu.org/licenses/.
 */

package be.fedict.eid.diagnostics;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;

import be.fedict.eid.diagnostics.helper.DataTreeBuilder;


/**
 * Interface for diagnostic tests 
 * 
 * @author Kevin Van Wilder
 *
 */
public abstract class DiagnosticTest implements Runnable {

	protected DefaultMutableTreeNode rootNode;
	private String name;
	private String problem;
	private RESULT result = RESULT.NONE;
	
	public MutableTreeNode getGatheredData() {
		return rootNode;
	}

	public String getName() {
		return name;
	}

	public String getProblem() {
		return problem; 
	}
	
	public static enum RESULT
    {
        NONE("result_none"), SUCCESS("result_success"), FAILED("result_fail"), SUCCESS_WITH_WARNINGS("result_with_warnings");
        private final String state;

        private RESULT(String state)
        {
            this.state = state;
        }

        @Override
        public String toString()
        {
            return this.state;
        }
    };
	
	public void setName(String testName) {
		this.name = testName;
	}   
    
    public RESULT getResult() {
    	return this.result;
    }
    
    
    public void run() {
    	// Create the root
    	rootNode = DataTreeBuilder.buildNode(this.getRootName(), "test");
    	try {
	    	if(runTest())
	    		result = RESULT.SUCCESS;
			else
				result = RESULT.SUCCESS_WITH_WARNINGS;
    	}
    	catch (TestFailedException e) {
    		problem = e.getMessage();
    		result = RESULT.FAILED;
    	}
    	catch (Exception e) {
    		result = RESULT.FAILED;
    		e.printStackTrace();
    	}
    }
    
    protected abstract boolean runTest() throws TestFailedException;
    
    protected abstract String getRootName();


}
